<?php

namespace App\Http\Controllers;

use App\Models\Alarm;
use App\Models\Monitor;
use App\Models\MonitorCharge;
use App\Models\Scene;
use DB;
use Illuminate\Support\Facades\Input;

class MonitorController extends AuthController
{

    public function index(){
        $monitors = Monitor::all();
        foreach ($monitors as $key=>$val){
            $val->scene_id = Scene::find($val->scene_id)->title;
        }
        return response()->json($monitors->groupBy('scene_id'));
    }

    public function alarm($title=''){
        $url = config('monitor_host').config('alarm_api');
        $file_contents = file_get_contents($url);
        $Channels = json_decode($file_contents)->EasyDarwin->Body->Channels;
        $alarm = collect($Channels)->where('Online',0);
        $channels = $alarm->values()->pluck('Channel');
        foreach ($alarm as $val){
            if($monitor = Monitor::where('channel_id',$val->Channel)->first()){
                Alarm::updateOrCreate(['channel_id'=>$val->Channel],[
                    'title'=>$monitor->title,
                    'scene_id'=>$monitor->scene_id,
                    'error'=>$val->ErrorString,
                ]);
                $monitor->online = 0;
                $monitor->save();
            }
        }
        Monitor::whereNotIn('channel_id',$channels)->update(['online' => 1,'status'=>0]);
        $alarms = Alarm::where('title','like','%'.$title.'%')->get(['title','channel_id','error','updated_at','scene_id']);
        foreach ($alarms as $val){
            $val->info = $val->getSceneArea();
            $val->charge = $val->getCharge();
            $val->monitor_id=Monitor::where('channel_id',$val->channel_id)->first()->id;
            $val->status = Monitor::where('channel_id',$val->channel_id)->first()->status;
        }

        return response()->json($alarms);
    }

    public function getAlarm(){
        $monitor_id = Input::get('monitor_id');
        $monitor = Monitor::find($monitor_id);
        if($monitor_id&&$monitor){
            $charge = MonitorCharge::where('scene_id',$monitor->scene_id)->first();
            if($charge&&$charge->manager_id!=null){
                DB::table('admin_messages')->insert([
                    'from'=>1,
                    'to'=>$charge->manager_id,
                    'monitor_id'=>$monitor_id,
                    'title'=>'监控报警',
                    'message'=>$monitor->title.'故障',
                ]);
                $monitor->status = 1;
                $monitor->save();
                return response()->json('报警成功');
            }else
                return response()->json('该景区还没有分配管理员账户');
        }else
            return response()->json('检查参数');
    }

    public function monitor(){
        $scene_id = Input::get('scene_id');
        if(Input::has('scene_id')){
            $where = $scene_id==0?[]:[['scene_id',$scene_id]];
            $list = Monitor::where($where)->get(['id','scene_id','cover','link','hls','online','latlng','title']);
            return response()->json($list);
        }else
            return response()->json(false);

    }

}
